package dailyTopic;

/**
 * @author zhangmin
 * @create 2021-07-16 9:42
 */
public class search53 {

    public int binarySearch(int[] nums,int target,boolean lower){
        int left = 0, right = nums.length - 1, ans = nums.length;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (nums[mid] > target || (lower && nums[mid] >= target)) {
                right = mid - 1;
                ans = mid;
            } else {
                left = mid + 1;
            }
        }
        return ans;
    }
    public int search(int[] nums, int target) {
       int lindex=binarySearch(nums,target,true);
       int rindex=binarySearch(nums,target,false)-1;
        System.out.println("l="+lindex);
        System.out.println("r="+rindex);
        if (lindex<=rindex&&rindex<nums.length&&nums[lindex]==target&&nums[rindex]==target){
            return rindex-lindex+1;
        }
        return 0;
    }

  /*  public static void main(String[] args) {
        int[] nums1=new int[]{2,2};
        //int[] nums2=new int[]{2,3,5};
        dailyTopic.search53 object=new dailyTopic.search53();
        System.out.println(object.search(nums1,2));
    }*/
}
